package com.yipei.business.controller;

import com.yipei.business.domain.MlAssetFlow;
import com.yipei.business.domain.MlTradeDealOrder;
import com.yipei.business.domain.MlTradeEntrustOrder;
import com.yipei.business.service.IMlTradeDealOrderService;
import com.yipei.common.annotation.Log;
import com.yipei.common.core.controller.BaseController;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.core.page.TableDataInfo;
import com.yipei.common.enums.BusinessType;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 成交订单Controller
 *
 * @author yipei
 * @date 2024-12-21
 */
@RestController
@RequestMapping("/business/dealorder")
@Api(tags = "成交订单", description = "成交订单")
public class MlTradeDealOrderController extends BaseController {
    @Autowired
    private IMlTradeDealOrderService mlTradeDealOrderService;

    /**
     * 查询成交订单列表
     */
    //@PreAuthorize("@ss.hasPermi('business:dealorder:list')")
    @ApiOperation("成交订单获取数据")
    @GetMapping("/list")
    public TableDataInfo list(MlTradeDealOrder mlTradeDealOrder) {
        startPage();
        List<MlTradeDealOrder> list = mlTradeDealOrderService.selectMlTradeDealOrderList(mlTradeDealOrder);
        return getDataTable(list);
    }

    /**
     * 导出成交订单列表
     */
    //@PreAuthorize("@ss.hasPermi('business:dealorder:export')")
    @ApiOperation("成交订单导出数据")
    @Log(title = "成交订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlTradeDealOrder mlTradeDealOrder) {
        List<MlTradeDealOrder> list = mlTradeDealOrderService.selectMlTradeDealOrderList(mlTradeDealOrder);
        ExcelUtil<MlTradeDealOrder> util = new ExcelUtil<MlTradeDealOrder>(MlTradeDealOrder.class);
        util.exportExcel(response, list, "成交订单数据");
    }

    /**
     * 获取成交订单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:dealorder:query')")
    @ApiOperation("成交订单通过ID获取数据")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mlTradeDealOrderService.selectMlTradeDealOrderById(id));
    }

    /**
     * 新增成交订单
     */
    //@PreAuthorize("@ss.hasPermi('business:dealorder:add')")
    @ApiOperation("成交订单新增数据")
    @Log(title = "成交订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MlTradeDealOrder mlTradeDealOrder) {
        return toAjax(mlTradeDealOrderService.insertMlTradeDealOrder(mlTradeDealOrder));
    }

    /**
     * 修改成交订单
     */
    //@PreAuthorize("@ss.hasPermi('business:dealorder:edit')")
    @ApiOperation("成交订单修改数据")
    @Log(title = "成交订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlTradeDealOrder mlTradeDealOrder) {
        return toAjax(mlTradeDealOrderService.updateMlTradeDealOrder(mlTradeDealOrder));
    }

    /**
     * 删除成交订单
     */
    //@PreAuthorize("@ss.hasPermi('business:dealorder:remove')")
    @ApiOperation("成交订单删除数据")
    @Log(title = "成交订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mlTradeDealOrderService.deleteMlTradeDealOrderByIds(ids));
    }

    @ApiOperation("获取今日成交单")
//    @Log(title = "委托订单", businessType = BusinessType.INSERT)
    @GetMapping("/getDealOrder")
    public AjaxResult getDealOrder() {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        SimpleDateFormat dateStr = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateStr.format(new Date());
        String e_time= date+" 24:59:59";
        String s_time=date+" 00:00:00";
        List<MlTradeDealOrder> list = mlTradeDealOrderService.lambdaQuery().eq(MlTradeDealOrder::getTdAcc,tdAcc)
                .ge(MlTradeDealOrder::getCreateTime,s_time).le(MlTradeDealOrder::getCreateTime,e_time).list();
        if(CollectionUtils.isEmpty(list)){
            return AjaxResult.error("未获取到今日成交订单！");
        }else {
            return AjaxResult.success(list);
        }
    }

    @ApiOperation("获取历史成交单据")
//    @Log(title = "委托订单", businessType = BusinessType.INSERT)
    @GetMapping("/dealHistoryOrder")
    public AjaxResult getDealHistoryOrder(@RequestParam(name = "s_time", required = true) String s_time,@RequestParam(name = "e_time", required = true) String e_time ) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        e_time= e_time+" 24:59:59";
        List<MlTradeDealOrder> list = mlTradeDealOrderService.lambdaQuery().eq(MlTradeDealOrder::getTdAcc,tdAcc)
                .ge(MlTradeDealOrder::getCreateTime,s_time).le(MlTradeDealOrder::getCreateTime,e_time)
                .list();
        if(CollectionUtils.isEmpty(list)){
            return AjaxResult.error("未获取到时间范围内成交订单！");
        }else {
            SimpleDateFormat dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (MlTradeDealOrder mlTradeDealOrder : list) {
                mlTradeDealOrder.setViewSj(dateStr.format(mlTradeDealOrder.getCjSj()*1000));
            }
            return AjaxResult.success(list);
        }
    }
}
